import 'dart:ui';
import 'package:dio/dio.dart';
import '../../../config/bt_env_config.dart';
import '../../models/bt_app_config.dart';
import '../../models/bt_block.dart';
import '../../models/bt_broadcaster.dart';
import '../../models/bt_empty_data.dart';
import '../../models/bt_friend.dart';
import '../../models/bt_goods.dart';
import '../../models/bt_login_result.dart';
import '../../models/bt_oss_policy.dart';
import '../../models/bt_recharge.dart';
import '../../models/bt_strategy.dart';
import '../../models/bt_update_avatar_result.dart';
import '../../models/bt_user_info.dart';
import '../bt_api_result.dart';
import '../bt_dio_http_formatter.dart';
import '../interceptor/bt_res_data_converter_interceptor.dart';
import '../interceptor/bt_req_headers_interceptor.dart';
import 'bt_dio_network_api.dart';
import 'bt_net_data_source.dart';

class BtDioNetwork implements BtNetDataSource {
  BtDioNetwork._internal() {
    Map<String, dynamic> headers = {};
    headers['kst'] = '1';
    headers['sys_lan'] = const Locale.fromSubtags().languageCode;
    headers['lang'] == const Locale.fromSubtags().languageCode;

    dio = Dio(
      BaseOptions(
          baseUrl: apiBaseUrl,
          connectTimeout: const Duration(seconds: 60),
          receiveTimeout: const Duration(seconds: 60),
          responseType: ResponseType.json,
          contentType: Headers.jsonContentType,
          headers: headers),
    );

    //access token拦截
    dio.interceptors.add(BtReqHeadersInterceptor());
    dio.interceptors.add(BtResDataConverterInterceptor());
    //日志拦截
    const bool inProduction = bool.fromEnvironment("dart.vm.product");
    if (!inProduction) {
      dio.interceptors.add(BtHttpFormatter());
    }

    networkApi = BtDioNetworkApi(dio);
  }

  factory BtDioNetwork() => _instance;

  static final BtDioNetwork _instance = BtDioNetwork._internal();

  late final Dio dio;
  late final BtDioNetworkApi networkApi;

  @override
  Future<BtApiResult<BtLoginResult>> login(String authorized, Map<String, dynamic> map) =>
      networkApi.login(authorized, map);

  @override
  Future<BtApiResult<BtAppConfig>> getAppConfig(String authorized, Map<String, dynamic> queries) =>
      networkApi.getAppConfig(authorized, queries);

  @override
  Future<BtApiResult<List<BtBroadcaster>>> fetchBroadcasterList(Map<String, dynamic> body) =>
      networkApi.fetchBroadcasterList(body);

  @override
  Future<BtApiResult<List<BtGoods>>> fetchCoinGoodsList(Map<String, dynamic> body) =>
      networkApi.fetchCoinGoodsList(body);

  @override
  Future<BtApiResult<bool>> deleteAccount(Map<String, dynamic> body) => networkApi.deleteAccount(body);

  @override
  Future<BtApiResult<BtEmptyData>> logout(Map<String, dynamic> body) => networkApi.logout(body);

  @override
  Future<BtApiResult<bool>> validToken(Map<String, dynamic> body) => networkApi.validToken(body);

  @override
  Future<BtApiResult<bool>> saveUserInfo(Map<String, dynamic> body) => networkApi.saveUserInfo(body);

  @override
  Future<BtApiResult<BtUserInfo>> getUserInfo(Map<String, dynamic> queries) =>
      networkApi.getUserInfo(queries);

  @override
  Future<BtApiResult<List<BtBlock>>> getBlockList(Map<String, dynamic> body) => networkApi.getBlockList(body);

  @override
  Future<BtApiResult<List<BtFriend>>> getFriendList(Map<String, dynamic> body) =>
      networkApi.getFriendList(body);

  @override
  Future<BtApiResult<bool>> removeBlock(Map<String, dynamic> body) => networkApi.removeBlock(body);

  @override
  Future<BtApiResult<BtOssPolicy>> getOssPolicy(Map<String, dynamic> queries) =>
      networkApi.getOssPolicy(queries);

  @override
  Future<BtApiResult<BtUpdateAvatarResult>> updateAvatar(Map<String, dynamic> body) =>
      networkApi.updateAvatar(body);

  @override
  Future<BtApiResult<BtRecharge>> recharge(Map<String, dynamic> body) => networkApi.recharge(body);

  @override
  Future<BtApiResult<bool>> rechargePayment(Map<String, dynamic> body) => networkApi.rechargePayment(body);

  @override
  Future<BtApiResult<BtStrategy>> getStrategy(Map<String, dynamic> queries) =>
      networkApi.getStrategy(queries);

  @override
  Future<BtApiResult<BtEmptyData>> getRongCloudToken(Map<String, dynamic> queries) =>
      networkApi.getRongCloudToken(queries);

  @override
  Future<BtApiResult<bool>> addFriend(Map<String, dynamic> body) => networkApi.addFriend(body);

  @override
  Future<BtApiResult<bool>> unfriend(Map<String, dynamic> body) => networkApi.unfriend(body);

  @override
  Future<BtApiResult<bool>> reportOrBlock(Map<String, dynamic> body) => networkApi.reportOrBlock(body);

  @override
  Future<BtApiResult<bool>> reviewModeConsume(Map<String, dynamic> body) =>
      networkApi.reviewModeConsume(body);
}
